package com.swind.fast.modules.app.wii.article.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.swind.fast.common.api.vo.Result;
import com.swind.fast.common.system.query.QueryGenerator;
import com.swind.fast.common.util.oConvertUtils;
import com.swind.fast.modules.app.wii.article.entity.WiiArticleInfo;
import com.swind.fast.modules.app.wii.article.service.IWiiArticleInfoService;

import com.swind.fast.modules.app.wii.article.entity.WiiArticleContent;
import com.swind.fast.modules.app.wii.article.service.IWiiArticleContentService;

import com.swind.fast.modules.app.wii.article.vo.WiiArticleVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.BeanUtils;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;
import java.util.Date;

@RestController
@RequestMapping("/app/wii/article")
@Slf4j
// @Api(tags="文章模块")
public class WiiArticleInfoController {

    @Autowired
    private IWiiArticleInfoService wiiArticleInfoService;

    @Autowired
    private IWiiArticleContentService wiiArticleContentService;

    // @ApiOperation("文章列表")
    @GetMapping(value = "/list")
    public Result<IPage<WiiArticleInfo>> queryPageList(WiiArticleInfo wiiArticleInfo,
                                      @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                      @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                      HttpServletRequest req) {

        QueryWrapper<WiiArticleInfo> queryWrapper = QueryGenerator.initQueryWrapper(wiiArticleInfo, req.getParameterMap());
        Page<WiiArticleInfo> page = new Page<WiiArticleInfo>(pageNo, pageSize);
        IPage<WiiArticleInfo> pageList = wiiArticleInfoService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    // @ApiOperation("创建文章")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody WiiArticleVo wiiArticleVo) {
        WiiArticleInfo wiiArticleInfo = new WiiArticleInfo();
        wiiArticleInfo.setTitle(wiiArticleVo.getTitle());
        wiiArticleInfoService.save(wiiArticleInfo);

        WiiArticleContent articleContent = new WiiArticleContent(wiiArticleInfo.getId(), wiiArticleVo.getContent());
        wiiArticleContentService.save(articleContent);

        return Result.OK("添加成功！", wiiArticleInfo);
    }

    // @ApiOperation("更新文章")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody WiiArticleVo wiiArticleVo) {
        String articleId = wiiArticleVo.getId();
        UpdateWrapper<WiiArticleInfo> updateWrapper = new UpdateWrapper<WiiArticleInfo>().set("title", wiiArticleVo.getTitle()).eq("id", articleId).last("limit 1");
        wiiArticleInfoService.update(updateWrapper);

        String content = wiiArticleVo.getContent();
        WiiArticleContent wiiArticleContent = new WiiArticleContent(articleId, content);
        UpdateWrapper<WiiArticleContent> pupdateWrapper = new UpdateWrapper<WiiArticleContent>().set("content", content).eq("article_id", articleId).last("limit 1");
        wiiArticleContentService.saveOrUpdate(wiiArticleContent, pupdateWrapper);

        return Result.OK("编辑成功!");
    }

    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id", required=true) String id) {
        // log.info("id: {}", id);

        wiiArticleInfoService.removeById(id);
        QueryWrapper<WiiArticleContent> queryWrapper = new QueryWrapper<WiiArticleContent>().select("content").eq("article_id", id).last("limit 1");
        wiiArticleContentService.remove(queryWrapper);
        return Result.OK("删除成功!");
    }

    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        WiiArticleInfo wiiArticleInfo = wiiArticleInfoService.getById(id);

        QueryWrapper<WiiArticleContent> queryWrapper = new QueryWrapper<WiiArticleContent>().select("content").eq("article_id", id).last("limit 1");
        WiiArticleContent wiiArticleContent = wiiArticleContentService.getOne(queryWrapper);
        WiiArticleVo resp = new WiiArticleVo(wiiArticleInfo.getId(), wiiArticleInfo.getTitle(), wiiArticleContent.getContent());

        return Result.ok(resp);
    }

    // @GetMapping(value = "/hello")
    // public Result<String> hello() {
    //     Result<String> result = new Result<String>();
    //     result.setResult("Hello World!");
    //     result.setSuccess(true);
    //     return result;
    // }

}
